package com.theeviljames.sorting;

import java.util.Arrays;

public class SelectionSort implements ISort{

	public int[] sort(int[] A) {
		int counter = 0;
		//Starting from the last element in the array (i)
		for(int i = A.length-1; i >= 1; i--){
			//Set a pointer to the last elements index and name this max
			int max = i;
			//Starting from the beginning of the array (j)
			for(int j = 0; j < i; j++){
				//If the value of A[max]<A[j] set max to be j
				counter++;
				if(A[max]<A[j])max = j;
			}
			//Exchange the element A[i] with A[max]
			int copy = A[i];
			A[i] = A[max];
			A[max] = copy;
			//System.out.println("" + i + "th index, " + (A.length-i) + "th iteration>" + Arrays.toString(A));
		}
		System.out.println("Iterations>" + counter);
		return A;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Selection Sort";
	}
}
